﻿@{
    var photoId = UrlData[0].AsInt();

    var db = Database.Open("PhotoGallery");

    var photo = db.QuerySingle("SELECT * FROM Photos WHERE Id = @0", photoId);
    if (photo == null) {
        Response.SetStatus(HttpStatusCode.NotFound);
        return;        
    }

    WebSecurity.RequireUser(photo.UserId);

    Page.Title = "Edit Photo - " + photo.FileTitle;

    if (IsPost) {
        var fileTitle = Request["FileTitle"];
        if (fileTitle.IsEmpty()) {
            ModelState.AddError("FileTitle", "You must specify a name");
        }
        var description = Request["Description"];
        if (description.IsEmpty()) {
            ModelState.AddError("Description", "You must specify a description");
        }
        if (ModelState.IsValid) {
            db.Execute("UPDATE Photos SET FileTitle = @0, Description= @1 WHERE Id = @2", fileTitle, description, photoId);
            Response.Redirect(Href("View", photo.Id));
        }
    }
}

<div class="sidebar">
    <h1>Edit Photo</h1>
    <form method="post" action="">
        <fieldset class="no-legend">
            <legend>Edit Photo</legend>
            <ol>
                <li>
                    <label for="fileTitle">Name:</label>
                    <input type="text" id="fileTitle" name="fileTitle" title="Photo name" value="@photo.FileTitle" placeholder="Photo Name" class="@PhotoGallery.GetValidationClass("fileTitle")" />
                    @Html.ValidationMessage("FileTitle")
                </li>
                <li>
                    <label for="description">Description:</label>
                    <textarea id="description" name="description" title="Photo description" rows="4" cols="30" placeholder="Photo description" class="@PhotoGallery.GetValidationClass("description")">@photo.Description</textarea>
                    @Html.ValidationMessage("Description")
                </li>
            </ol>
            <p class="form-actions">
                <input type="submit" value="Save Changes" title="Save changes" />
                <a href="@Href("~/Photo/View", photoId)" title="Cancel changes">Cancel</a>
            </p>
        </fieldset>
    </form>
</div>
<div class="main">
    <img alt="@Html.AttributeEncode(photo.FileTitle)" src="@Href("~/Photo/Thumbnail", photo.Id, new { size="large" })" />
</div>